package com.tuanzi.loan.web.bean.banana.statistics;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.banana.statistics.BananaCommonActivityRegisterRequest;
import com.tuanzi.loan.vo.response.banana.statistics.BananaCommonActivityRegisterResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.banana.statistics.BananaCommonActivityRegisterLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用活动注册统计
 *
 * @author yuzhenhua 2018/07/19
 *
 */

@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class BananaCommonActivityRegisterBean extends JSFListingBean<BananaCommonActivityRegisterRequest, BananaCommonActivityRegisterResponse> {

	private List<CommProductId> bananaProducts;
	private List<CommProductChannel> bananaProductChannels;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	public void onReset() {
		init();
	}

	private void initLazyDataModel() {
		$ = new BananaCommonActivityRegisterRequest();
		$.setStartDate(new DateTime().plusDays(-1).toDate());
		$.setEndDate(new Date());
		lazyDataModel = new BananaCommonActivityRegisterLazyDataModel($);

		bananaProducts = getCommProductIds().parallelStream().filter(x -> StringUtils.equalsIgnoreCase(x.getParentId(), "3")).collect(Collectors.toList());

		List<Integer> bananaProductIds = bananaProducts.parallelStream().map(x -> x.getProductId()).collect(Collectors.toList());
		bananaProductChannels = getCommProductChannels().parallelStream().filter(x -> bananaProductIds.contains(x.getProductId())).collect(Collectors.toList());
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(DictionaryCode.CHANNEL_GROUP);
	}

}